package com.qf.service.impl;

import com.qf.bean.User;
import com.qf.dao.UserDao;
import com.qf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * IntelliJ IDEA -> member
 * 2019/12/3 14:30
 *
 * @author zmyao
 * 别看，我是渣渣。
 */
@Service
public class UserServiceImpl  implements UserService {
@Autowired
private UserDao userDao;
    @Override
    public Page<User> getUsers(Integer pageIndex, Integer pageSize,String name ) {
        if(name == null){
           name="";
        }
        PageRequest pageRequest = new PageRequest(pageIndex - 1, pageSize);
        Page<User> users = userDao.findByNameContaining(name, pageRequest);
        return  users;
    }

    @Override
    public User login(String name) {
        return  userDao.findByName(name);

    }

    @Transactional
    @Override
    public int updatePassword(User user,String oldPassword,String newPassword) {
        String password = user.getPassword();
        //原始密码输入的不对
        if (!password.equals(oldPassword)){
            return  -1;
        }
        //新密码和旧密码相等
        else if (oldPassword.equals(newPassword)){
            return  -2;
        }
        user.setPassword(newPassword);
        User user1 = userDao.save(user);
        //操作失败
        if (user1 == null ){
            return  0;
        }
        //操作成功
        return  1;
    }
}
